package com.rhzy.wnhc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rhzy.wnhc.entity.AdminParam;
import com.rhzy.wnhc.entity.LoginVo;
import com.rhzy.wnhc.entity.TAdmin;
import com.rhzy.wnhc.entity.TJobWish;
import com.rhzy.wnhc.service.TAdminService;
import com.rhzy.wnhc.util.AjaxResult;
import com.rhzy.wnhc.util.CheckIdcardIsLegal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wen
 * @since 2020-02-22
 */
@Api(tags = "认证管理")
@RestController
@RequestMapping("/auth")
public class TAdminController {

    @Autowired
    TAdminService adminService;

    @ApiOperation(value = "登录", tags = {"认证管理",})
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @Transactional
    public AjaxResult addWishJob(@RequestBody @Validated LoginVo admin) {
        try {
            TAdmin tAdmin = adminService.getOne(new LambdaQueryWrapper<TAdmin>().eq(TAdmin::getUsername, admin.getUsername()).eq(TAdmin::getPassword, admin.getPassword()));
            if (tAdmin == null) {
                return AjaxResult.addError("用户名密码错误");
            }
            return AjaxResult.addSuccess("登录成功");

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.addError("异常：" + e.getMessage());
        }
    }


    @ApiOperation(value = "添加管理员", tags = {"认证管理",})
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Transactional
    public AjaxResult save(@RequestBody @Validated LoginVo admin) {
        try {
            TAdmin tAdmin = new TAdmin();
            BeanUtils.copyProperties(admin, tAdmin);
            boolean res = adminService.save(tAdmin);
            if (res) {
                return AjaxResult.addSuccess("添加成功！");
            } else {
                return AjaxResult.addError("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.addError("异常：" + e.getMessage());
        }
    }


    @ApiOperation(value = "获取管理员列表", tags = {"认证管理",})
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @Transactional
    public AjaxResult pageList(@RequestBody AdminParam adminParam) {
        LambdaQueryWrapper<TAdmin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(TAdmin::getUsername,TAdmin::getUname,TAdmin::getPhone).orderByDesc(TAdmin::getId);
        queryWrapper.likeLeft(!StringUtils.isEmpty(adminParam.getAreacode()), TAdmin::getAreacode, adminParam.getAreacode());
        queryWrapper.like(!StringUtils.isEmpty(adminParam.getUname()), TAdmin::getUname, adminParam.getUname());
        queryWrapper.like(!StringUtils.isEmpty(adminParam.getPhone()), TAdmin::getPhone, adminParam.getPhone());
        Page<TAdmin> page = new Page<>(adminParam.getPageIndex(), adminParam.getPageSize());  // 查询第1页，每页返回10条
        IPage<TAdmin> iPage = adminService.page(page, queryWrapper);
        return AjaxResult.addSuccess(iPage, "查询成功！");
    }

}
